<?php

namespace app\controller\admin\sky;

use app\model\api\SkyActivity;
use app\model\api\SkyTask;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;
use think\response\Json;

/**
 * Class Task
 * @package app\controller\admin\sky
 */
class Task extends Backend
{
    /**
     *
     * @var Task
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['sort' => 'desc', 'date' => 'desc'];

    public function _initialize()
    {
        $this->model = new SkyTask();
    }


    //添加
    public function add()
    {
        try {
            $post     = $this->request->post();
            if(empty($post['date'])){
                throw new \Exception('请选择日期');
            }

            $taskDesc = [];
            if(!empty($post['season_candle_location'])){
                $taskDesc['season_candle'] = [
                    'image' => !empty($post['season_candle_image'])?$post['season_candle_image']:'',
                    'location' => $post['season_candle_location'],
                    'title' => '今日季节蜡烛位置',
                ];
            }

            if(!empty($post['big_candle_location'])){
                $taskDesc['big_candle'] = [
                    'image' => !empty($post['big_candle_image'])?$post['big_candle_image']:'',
                    'location' => $post['big_candle_location'],
                    'title' => '今日大蜡烛位置',
                ];
            }

            $daily_tasks = [];
            for($i = 1;$i<=6;$i++){
                if(!empty($post['task_name'.$i])){
                    $daily_tasks[] = array(
                        'image' => !empty($post['task_image'.$i])?$post['task_image'.$i]:'',
                        'name' => $post['task_name'.$i],
                        'description' => !empty($post['task_description'.$i])?$post['task_description'.$i]:''
                    );
                }
            }
            $taskDesc['daily_tasks'] = $daily_tasks;
            $saveData = [
                'date'      => $post['date'],
                'sort'      => 1,
                'task_name' => $post['task_name'],
                'task_desc' => json_encode($taskDesc,JSON_UNESCAPED_UNICODE),
                'status'    => 1,
                'create_time' => time()
            ];

            $saveAll = $this->model->save($saveData);
            if (!$saveAll) throw new \Exception('保存失败');

            $this->clearCache();
            return $this->success('操作成功');
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }


    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');

        if(empty($post['date'])){
            throw new \Exception('请选择日期');
        }

        $taskDesc = [];
        if(!empty($post['season_candle_location'])){
            $taskDesc['season_candle'] = [
                'image' => !empty($post['season_candle_image'])?$post['season_candle_image']:'',
                'location' => $post['season_candle_location'],
                'title' => '今日季节蜡烛位置',
            ];
        }

        if(!empty($post['big_candle_location'])){
            $taskDesc['big_candle'] = [
                'image' => !empty($post['big_candle_image'])?$post['big_candle_image']:'',
                'location' => $post['big_candle_location'],
                'title' => '今日大蜡烛位置',
            ];
        }

        $daily_tasks = [];
        for($i = 1;$i<=6;$i++){
            if(!empty($post['task_name'.$i])){
                $daily_tasks[] = array(
                    'image' => !empty($post['task_image'.$i])?$post['task_image'.$i]:'',
                    'name' => $post['task_name'.$i],
                    'description' => !empty($post['task_description'.$i])?$post['task_description'.$i]:''
                );
            }
        }
        $taskDesc['daily_tasks'] = $daily_tasks;

        $row->date = $post['date'];
        $row->task_name = $post['task_name'];
        $row->task_desc = json_encode($taskDesc,JSON_UNESCAPED_UNICODE);
        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }


    public function info(){
        $id = $this->request->param('id');

        $task = $this->model->where('id',$id)->find()->toArray();
        $task['task_desc'] = json_decode($task['task_desc'],true);

        $task['season_candle_location'] = !empty($task['task_desc']['season_candle']['location'])?$task['task_desc']['season_candle']['location']:'';
        $task['season_candle_image'] = !empty($task['task_desc']['season_candle']['image'])?$task['task_desc']['season_candle']['image']:'';

        $task['big_candle_location'] = !empty($task['task_desc']['big_candle']['location'])?$task['task_desc']['big_candle']['location']:'';
        $task['big_candle_image'] = !empty($task['task_desc']['big_candle']['image'])?$task['task_desc']['big_candle']['image']:'';

        for($i=1;$i<=6;$i++){
            $task['task_name'.$i] = '';
            $task['task_description'.$i] = '';
            $task['task_image'.$i] = '';
        }

        if(!empty($task['task_desc']['daily_tasks'])){
            foreach($task['task_desc']['daily_tasks'] as $k => $v){
                $index = $k + 1;
                $task['task_name'.$index] = $v['name'];
                $task['task_description'.$index] = $v['description'];
                $task['task_image'.$index] = $v['image'];
            }
        }
        return $this->success('获取成功', $task);
    }
}